<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Standard Placeholders - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/learning.view.placeholders.standard.html">Inglês (English)</a></li>
    <li><a href="../pt-br/learning.view.placeholders.standard.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.view.placeholders.basics.html">Basic Placeholder Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.view.placeholders.html">Getting Started Zend_View Placeholders</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.view.placeholders.conclusion.html">View Placeholders: Conclusion</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="learning.view.placeholders.standard" class="section"><div class="info"><h1 class="title">Standard Placeholders</h1></div>
    

    <p class="para">
        In the <a href="learning.view.placeholders.basics.html" class="link">previous section</a>, we learned
        about the  <span class="methodname">placeholder()</span> view helper, and how it can be used to
        aggregate custom content. In this section, we&#039;ll look at some of the concrete placeholders
        shipped with Zend Framework, and how you can use them to your advantage when creating
        complex composite layouts.
    </p>

    <p class="para">
        Most of the shipped placeholders are for generating content for the
        <em class="emphasis">&lt;head&gt;</em> section of your layout content -- an area you typically
        cannot manipulate directly via your application view scripts, but one you may want to
        influence. As examples: you may want your title to contain certain content on every page,
        but specific content based on the controller and/or action; you may want to specify
        <acronym class="acronym">CSS</acronym> files to load based on what section of the application you&#039;re in; you
        may need specific JavaScript scripts loaded at different times; or you may want to set the
        <em class="emphasis">DocType</em> declaration.
    </p>

    <p class="para">
        Zend Framework ships with placeholder implementations for each of these situations, and
        several more.
    </p>

    <div class="section" id="learning.view.placeholders.standard.doctype"><div class="info"><h1 class="title">Setting the DocType</h1></div>
        

        <p class="para">
            <em class="emphasis">DocType</em> declarations are troublesome to memorize, and often
            essential to include in your document to ensure the browser properly renders your
            content. The  <span class="methodname">doctype()</span> view helper allows you to use simple
            string mnemonics to specify the desired <em class="emphasis">DocType</em>; additionally,
            other helpers will query the  <span class="methodname">doctype()</span> helper to ensure the
            output generated conforms with the requested <em class="emphasis">DocType</em>.
        </p>

        <p class="para">
            As an example, if you want to use the <acronym class="acronym">XHTML1</acronym> Strict
            <acronym class="acronym">DTD</acronym>, you can simply specify:
        </p>

        <pre class="programlisting brush: php">
$this-&gt;doctype(&#039;XHTML1_STRICT&#039;);
</pre>


        <p class="para">
            Among the other available mnemonics, you&#039;ll find these common types:
        </p>

        <dl>

            <dt>

                <dt class="term">XHTML1_STRICT</dt>

                <dd>

                    <p class="para">
                        <acronym class="acronym">XHTML</acronym> 1.0 Strict
                    </p>
                </dd>

            </dt>


            <dt>

                <dt class="term">XHTML1_TRANSITIONAL</dt>

                <dd>

                    <p class="para">
                        <acronym class="acronym">XHTML</acronym> 1.0 Transitional
                    </p>
                </dd>

            </dt>


            <dt>

                <dt class="term">HTML4_STRICT</dt>

                <dd>

                    <p class="para">
                        <acronym class="acronym">HTML</acronym> 4.01 Strict
                    </p>
                </dd>

            </dt>


            <dt>

                <dt class="term">HTML4_Loose</dt>

                <dd>

                    <p class="para">
                        <acronym class="acronym">HTML</acronym> 4.01 Loose
                    </p>
                </dd>

            </dt>


            <dt>

                <dt class="term">HTML5</dt>

                <dd>

                    <p class="para">
                        <acronym class="acronym">HTML</acronym> 5
                    </p>
                </dd>

            </dt>

        </dl>


        <p class="para">
            You can assign the type and render the declaration in a single call:
        </p>

        <pre class="programlisting brush: php">
echo $this-&gt;doctype(&#039;XHTML1_STRICT&#039;);
</pre>


        <p class="para">
            However, the better approach is to assign the type in your bootstrap, and then render it
            in your layout. Try adding the following to your bootstrap class:
        </p>

        <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDocType()
    {
        $this-&gt;bootstrap(&#039;View&#039;);
        $view = $this-&gt;getResource(&#039;View&#039;);
        $view-&gt;doctype(&#039;XHTML1_STRICT&#039;);
    }
}
</pre>


        <p class="para">
            Then, in your layout script, simply  <span class="methodname">echo()</span> the helper at the
            top of the file:
        </p>

        <pre class="programlisting brush: php">
&lt;?php echo $this-&gt;doctype() ?&gt;
&lt;html&gt;
    &lt;!-- ... --&gt;
</pre>


        <p class="para">
            This will ensure that your DocType-aware view helpers render the appropriate markup,
            ensure that the type is set well before the layout is rendered, and provide a single
            location to change the DocType.
        </p>
    </div>

    <div class="section" id="learning.view.placeholders.standard.head-title"><div class="info"><h1 class="title">Specifying the Page Title</h1></div>
        

        <p class="para">
            Often, a site will include the site or business name as part of the page title, and
            then add additional information based on the location within the site. As an example,
            the <var class="filename">zend.com</var> website includes the string
            &quot;<var class="filename">Zend.com</var>&quot; on all pages, and the prepends information based on the
            page: &quot;Zend Server - <var class="filename">Zend.com</var>&quot;. Within Zend Framework, the
             <span class="methodname">headTitle()</span> view helper can help simplify this task.
        </p>

        <p class="para">
            At its simplest, the  <span class="methodname">headTitle()</span> helper allows you to aggregate
            content for the <em class="emphasis">&lt;title&gt;</em> tag; when you echo it, it then
            assembles it based on the order in which segments are added. You can control the order
            using  <span class="methodname">prepend()</span> and  <span class="methodname">append()</span>, and
            provide a separator to use between segments using the
             <span class="methodname">setSeparator()</span> method.
        </p>

        <p class="para">
            Typically, you should specify any segments common to all pages in your bootstrap,
            similar to how we define the doctype. In this case, we&#039;ll define a
             <span class="methodname">_initPlaceholders()</span> method for operating on all the various
            placeholders, and specify an initial title as well as a separator.
        </p>

        <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    // ...

    protected function _initPlaceholders()
    {
        $this-&gt;bootstrap(&#039;View&#039;);
        $view = $this-&gt;getResource(&#039;View&#039;);
        $view-&gt;doctype(&#039;XHTML1_STRICT&#039;);

        // Set the initial title and separator:
        $view-&gt;headTitle(&#039;My Site&#039;)
             -&gt;setSeparator(&#039; :: &#039;);
    }

    // ...
}
</pre>


        <p class="para">
            Within a view script, we might want to add another segment:
        </p>

        <pre class="programlisting brush: php">
&lt;?php $this-&gt;headTitle()-&gt;append(&#039;Some Page&#039;); // place after other segments ?&gt;
&lt;?php $this-&gt;headTitle()-&gt;prepend(&#039;Some Page&#039;); // place before ?&gt;
</pre>


        <p class="para">
            In our layout, we will simply echo the  <span class="methodname">headTitle()</span> helper:
        </p>

        <pre class="programlisting brush: php">
&lt;?php echo $this-&gt;doctype() ?&gt;
&lt;html&gt;
    &lt;?php echo $this-&gt;headTitle() ?&gt;
    &lt;!-- ... --&gt;
</pre>


        <p class="para">
            This will generate the following output:
        </p>

        <pre class="programlisting brush: html">
&lt;!-- If append() was used: --&gt;
&lt;title&gt;My Site :: Some Page&lt;/title&gt;

&lt;!-- If prepend() was used: --&gt;
&lt;title&gt;Some Page :: My Site&lt;/title&gt;
</pre>

    </div>

    <div class="section" id="learning.view.placeholders.standard.head-link"><div class="info"><h1 class="title">Specifying Stylesheets with HeadLink</h1></div>
        

        <p class="para">
            Good <acronym class="acronym">CSS</acronym> developers will often create a general stylesheet for
            sitewide styles, and individual stylesheets for specific sections or pages of the
            website, and load these latter conditionally so as to decrease the amount of data
            needing to be transferred on each request. The  <span class="methodname">headLink()</span>
            placeholder makes such conditional aggregation of stylesheets trivial within your
            application.
        </p>

        <p class="para">
            To accomplish this,  <span class="methodname">headLink()</span> defines a number of &quot;virtual&quot;
            methods (via overloading) to make the process trivial. The ones we will be concerned
            with are  <span class="methodname">appendStylesheet()</span> and
             <span class="methodname">prependStylesheet()</span>. Each takes up to four arguments,
            <var class="varname">$href</var> (the relative path to the stylesheet),
            <var class="varname">$media</var> (the <acronym class="acronym">MIME</acronym> type, which defaults to
            &quot;text/css&quot;), <var class="varname">$conditionalStylesheet</var> (which can be used to specify a
            &quot;condition&quot; under which the stylesheet will be evaluated), and
            <var class="varname">$extras</var> (an associative array of key and value pairs, commonly used
            to specify a key for &quot;media&quot;). In most cases, you will only need to specify the first
            argument, the relative path to the stylesheet.
        </p>

        <p class="para">
            In our example, we&#039;ll assume that all pages need to load the stylesheet located in
            &quot;<var class="filename">/styles/site.css</var>&quot; (relative to the document root); we&#039;ll specify
            this in our  <span class="methodname">_initPlaceholders()</span> bootstrap method.
        </p>

        <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    // ...

    protected function _initPlaceholders()
    {
        $this-&gt;bootstrap(&#039;View&#039;);
        $view = $this-&gt;getResource(&#039;View&#039;);
        $view-&gt;doctype(&#039;XHTML1_STRICT&#039;);

        // Set the initial title and separator:
        $view-&gt;headTitle(&#039;My Site&#039;)
             -&gt;setSeparator(&#039; :: &#039;);

        // Set the initial stylesheet:
        $view-&gt;headLink()-&gt;prependStylesheet(&#039;/styles/site.css&#039;);
    }

    // ...
}
</pre>


        <p class="para">
            Later, in a controller or action-specific view script, we can add more stylesheets:
        </p>

        <pre class="programlisting brush: php">
&lt;?php $this-&gt;headLink()-&gt;appendStylesheet(&#039;/styles/user-list.css&#039;) ?&gt;
</pre>


        <p class="para">
            Within our layout view script, once again, we simply echo the placeholder:
        </p>

        <pre class="programlisting brush: php">
&lt;?php echo $this-&gt;doctype() ?&gt;
&lt;html&gt;
    &lt;?php echo $this-&gt;headTitle() ?&gt;
    &lt;?php echo $this-&gt;headLink() ?&gt;
    &lt;!-- ... --&gt;
</pre>


        <p class="para">
            This will generate the following output:
        </p>

        <pre class="programlisting brush: html">
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/styles/site.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/styles/user-list.css&quot; /&gt;
</pre>

    </div>

    <div class="section" id="learning.view.placeholders.standard.head-script"><div class="info"><h1 class="title">Aggregating Scripts Using HeadScript</h1></div>
        

        <p class="para">
            Another common tactic to prevent long page load times is to only load JavaScript when
            necessary. That said, you may need several layers of scripts: perhaps one for
            progressively enhancing menus on the site, and another for page-specific content. In
            these situations, the  <span class="methodname">headScript()</span> helper presents a solution.
        </p>

        <p class="para">
            Similar to the  <span class="methodname">headLink()</span> helper,
             <span class="methodname">headScript()</span> provides the ability to append or prepend scripts
            to the collection, and then echo the entire set. It provides the flexibility to specify
            either script files themselves to load, or explicit JavaScript. You also have the option
            of capturing JavaScript via
             <span class="methodname">captureStart()</span>/ <span class="methodname">captureEnd()</span>, which
            allows you to simply inline the JavaScript instead of requiring an additional call to
            your server.
        </p>

        <p class="para">
            Also like  <span class="methodname">headLink()</span>,  <span class="methodname">headScript()</span>
            provides &quot;virtual&quot; methods via overloading as a convenience when specifying items to
            aggregate; common methods include  <span class="methodname">prependFile()</span>,
             <span class="methodname">appendFile()</span>,  <span class="methodname">prependScript()</span>, and
             <span class="methodname">appendScript()</span>. The first two allow you to specify files that
            will be referenced in a <em class="emphasis">&lt;script&gt;</em> tag&#039;s
            <var class="varname">$src</var> attribute; the latter two will take the content provided and
            render it as literal JavaScript within a <em class="emphasis">&lt;script&gt;</em> tag.
        </p>

        <p class="para">
            In this example, we&#039;ll specify that a script, &quot;<var class="filename">/js/site.js</var>&quot; needs
            to be loaded on every page; we&#039;ll update our
             <span class="methodname">_initPlaceholders()</span> bootstrap method to do this.
        </p>

        <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    // ...

    protected function _initPlaceholders()
    {
        $this-&gt;bootstrap(&#039;View&#039;);
        $view = $this-&gt;getResource(&#039;View&#039;);
        $view-&gt;doctype(&#039;XHTML1_STRICT&#039;);

        // Set the initial title and separator:
        $view-&gt;headTitle(&#039;My Site&#039;)
             -&gt;setSeparator(&#039; :: &#039;);

        // Set the initial stylesheet:
        $view-&gt;headLink()-&gt;prependStylesheet(&#039;/styles/site.css&#039;);

        // Set the initial JS to load:
        $view-&gt;headScript()-&gt;prependFile(&#039;/js/site.js&#039;);
    }

    // ...
}
</pre>


        <p class="para">
            Within a view script, we might then add an extra script file to source, or capture some
            JavaScript to include in our document.
        </p>

        <pre class="programlisting brush: php">
&lt;?php $this-&gt;headScript()-&gt;appendFile(&#039;/js/user-list.js&#039;) ?&gt;
&lt;?php $this-&gt;headScript()-&gt;captureStart() ?&gt;
site = {
    baseUrl: &quot;&lt;?php echo $this-&gt;baseUrl() ?&gt;&quot;
};
&lt;?php $this-&gt;headScript()-&gt;captureEnd() ?&gt;
</pre>


        <p class="para">
            Within our layout script, we then simply echo the placeholder, just as we have all the
            others:
        </p>

        <pre class="programlisting brush: php">
&lt;?php echo $this-&gt;doctype() ?&gt;
&lt;html&gt;
    &lt;?php echo $this-&gt;headTitle() ?&gt;
    &lt;?php echo $this-&gt;headLink() ?&gt;
    &lt;?php echo $this-&gt;headScript() ?&gt;
    &lt;!-- ... --&gt;
</pre>


        <p class="para">
            This will generate the following output:
        </p>

        <pre class="programlisting brush: html">
&lt;script type=&quot;text/javascript&quot; src=&quot;/js/site.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;/js/user-list.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
site = {
    baseUrl: &quot;&lt;?php echo $this-&gt;baseUrl() ?&gt;&quot;
};
&lt;/script&gt;
</pre>


        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>InlineScript Variant</b><br /></span>
            

            <p class="para">
                Many browsers will often block display of a page until all scripts and stylesheets
                referenced in the <em class="emphasis">&lt;head&gt;</em> section have loaded. If you have
                a number of such directives, this can impact how soon somebody can start actually
                viewing the page.
            </p>

            <p class="para">
                One way around this is to emit your <em class="emphasis">&lt;script&gt;</em> tags just
                prior to closing the <em class="emphasis">&lt;body&gt;</em> of your document. (This is a
                practice specifically recommend by the <a href="http://developer.yahoo.com/yslow/" class="link external">&raquo; Y! Slow project</a>.)
            </p>

            <p class="para">
                Zend Framework supports this in two different ways:
            </p>

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        You can render your  <span class="methodname">headScript()</span> tag whereever you
                        like in your layout script; just because the title references &quot;head&quot; does
                        not mean it needs to be rendered in that location.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        Alternately, you may use the  <span class="methodname">inlineScript()</span> helper,
                        which is simply a variant on  <span class="methodname">headScript()</span>, and
                        retains the same behavior, but uses a separate registry.
                    </p>
                </li>
            </ul>
        </p></blockquote>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.view.placeholders.basics.html">Basic Placeholder Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.view.placeholders.html">Getting Started Zend_View Placeholders</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.view.placeholders.conclusion.html">View Placeholders: Conclusion</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="learning.html">Learning Zend Framework</a></li>
  <li class="header up"><a href="learning.view.placeholders.html">Getting Started Zend_View Placeholders</a></li>
  <li><a href="learning.view.placeholders.intro.html">Introduction</a></li>
  <li><a href="learning.view.placeholders.basics.html">Basic Placeholder Usage</a></li>
  <li class="active"><a href="learning.view.placeholders.standard.html">Standard Placeholders</a></li>
  <li><a href="learning.view.placeholders.conclusion.html">View Placeholders: Conclusion</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>